﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.UI;
using Designer;
using FlexCell;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;
using YJKExcelTableUtil;
using YJKRevitKernel.ParameterEditUtility;

namespace YJKRoomPractice
{
	public partial class RoomPracticeForm : System.Windows.Forms.Form
	{
		public RoomPracticeForm(ExternalCommandData cmdData, Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			this.m_cmdData = cmdData;
			this.m_nVersionNumber = Convert.ToInt32(this.m_cmdData.Application.Application.VersionNumber);
			this.m_doc = doc;
			this.m_Uiapp = cmdData.Application;
			RoomPracticeForm.m_lstRoomData = new List<RoomData>();
			RoomPracticeForm.m_dicFloorToRowNumber = new Dictionary<string, List<int>>();
			this.GetRoom();
			if (this.m_lstElems.Count == 0 && this.m_lstLinkRooms.Count == 0)
			{
				this.m_bIsExit = false;
				return;
			}
			this.InitFont();
			this.InitFontSize();
			this.InitFlexCell();
			this.cbtnBackColor.Value = ColorTranslator.ToWin32(System.Drawing.Color.White);
			this.toolTip1.SetToolTip(this.cbtnBackColor, "背景色");
			this.cbtnFontColor.Value = ColorTranslator.ToWin32(System.Drawing.Color.Black);
			this.toolTip1.SetToolTip(this.cbtnFontColor, "文字颜色");
			this.toolTip1.SetToolTip(this.abtnAlign, "对齐方式");
			this.toolTip1.SetToolTip(this.bbtnBorder, "边框样式");
		}

		private void InitFont()
		{
			foreach (FontFamily fontFamily in FontFamily.Families)
			{
				this.toolCmbFont.ComboBox.Items.Add(fontFamily.Name);
			}
			this.toolCmbFont.Text = "宋体";
		}

		private void InitFontSize()
		{
			foreach (string item in new List<string>
			{
				"6",
				"8",
				"9",
				"10",
				"11",
				"12",
				"14",
				"16",
				"18",
				"20",
				"22",
				"24",
				"26",
				"28",
				"36",
				"48",
				"72"
			})
			{
				this.toolCmbFontSize.ComboBox.Items.Add(item);
			}
		}

		private void InitFlexCell()
		{
			short width = 200;
			short width2 = 150;
			this.grid.Row(0).Visible = false;
			this.grid.Column(0).Width = 1;
			this.grid.AllowUserResizing = ResizeEnum.Both;
			this.grid.Cols = 14;
			this.SetFlexCellFormat(1, 1, "楼层", 2, 1);
			this.SetFlexCellFormat(1, 2, "房间名称", 2, 2);
			this.SetFlexCellFormat(1, 3, "楼、地面", 1, 4);
			this.SetFlexCellFormat(2, 3, "名称", 0, 0);
			this.SetFlexCellFormat(2, 4, "编号", 0, 0);
			this.SetFlexCellFormat(1, 5, "踢脚", 1, 6);
			this.SetFlexCellFormat(2, 5, "名称", 0, 0);
			this.SetFlexCellFormat(2, 6, "编号", 0, 0);
			this.SetFlexCellFormat(1, 7, "墙裙", 1, 8);
			this.SetFlexCellFormat(2, 7, "名称", 0, 0);
			this.SetFlexCellFormat(2, 8, "编号", 0, 0);
			this.SetFlexCellFormat(1, 9, "内墙面", 1, 10);
			this.SetFlexCellFormat(2, 9, "名称", 0, 0);
			this.SetFlexCellFormat(2, 10, "编号", 0, 0);
			this.SetFlexCellFormat(1, 11, "顶棚", 1, 12);
			this.SetFlexCellFormat(2, 11, "名称", 0, 0);
			this.SetFlexCellFormat(2, 12, "编号", 0, 0);
			this.SetFlexCellFormat(1, 13, "备注", 2, 13);
			this.grid.Column(2).Width = width2;
			this.grid.Column(3).Width = width;
			this.grid.Column(5).Width = width;
			this.grid.Column(7).Width = width;
			this.grid.Column(9).Width = width;
			this.grid.Column(11).Width = width;
			this.grid.Column(13).Width = width;
			this.grid.Row(1).Locked = true;
			this.grid.Row(2).Locked = true;
			Range range = this.grid.Range(1, 1, this.grid.Rows - 1, this.grid.Cols - 1);
			range.FontName = "仿宋";
			if (this.m_nVersionNumber == 2016)
			{
				range.FontSize = 12f;
			}
			else
			{
				range.FontSize = 10f;
			}
			this.grid.Cell(1, 1).SetFocus();
		}

		private void GetRoom()
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfCategory(BuiltInCategory.OST_Rooms);
			foreach (Element element in filteredElementCollector)
			{
				Room room = (Room)element;
				string empty = string.Empty;
				if (room.get_Parameter(BuiltInParameter.ROOM_AREA).AsDouble() != 0.0)
				{
					this.m_lstElems.Add(room);
				}
			}
			this.HandleLinkDocument();
		}

		private void HandleLinkDocument()
		{
			List<Document> list = new List<Document>();
			IEnumerable<Document> linkDocuments = this.m_doc.GetLinkDocuments();
			if (linkDocuments == null)
			{
				return;
			}
			foreach (Autodesk.Revit.DB.Document document in linkDocuments)
			{
				if (document != null)
				{
					list.Add(document);
				}
			}
			if (list.Count == 0)
			{
				return;
			}
			foreach (Autodesk.Revit.DB.Document document2 in linkDocuments)
			{
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document2);
				filteredElementCollector.OfCategory(BuiltInCategory.OST_Rooms);
				foreach (Element element in filteredElementCollector)
				{
					Room room = (Room)element;
					string empty = string.Empty;
					if (room.get_Parameter(BuiltInParameter.ROOM_AREA).AsDouble() != 0.0)
					{
						this.m_lstLinkRooms.Add(room);
					}
				}
			}
		}

		private void ReadFormData()
		{
			string path = Path.GetTempPath() + "RoomPracticeForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			this.toolCmbFont.SelectedIndex = Convert.ToInt32(streamReader.ReadLine());
			this.toolCmbFontSize.SelectedIndex = Convert.ToInt32(streamReader.ReadLine());
			this.grid.Range(0, 0, this.grid.Rows - 1, this.grid.Cols - 1).FontName = this.toolCmbFont.Text;
			if (this.toolCmbFontSize.Text != "")
			{
				this.grid.Range(0, 0, this.grid.Rows - 1, this.grid.Cols - 1).FontSize = (float)int.Parse(this.toolCmbFontSize.Text);
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void WriteFormData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "RoomPracticeForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.toolCmbFont.SelectedIndex);
			streamWriter.WriteLine(this.toolCmbFontSize.SelectedIndex);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void SetFlexCellFormat(int nFirstRow, int nFirstCol, string strName, int nLastRow = 0, int nLastCol = 0)
		{
			Cell cell = this.grid.Cell(nFirstRow, nFirstCol);
			cell.Text = strName;
			cell.ForeColor = System.Drawing.Color.Red;
			cell.Alignment = AlignmentEnum.CenterCenter;
			if (strName != "名称" || strName != "编号")
			{
				this.grid.Range(nFirstRow, nFirstCol, nLastRow, nLastCol).Merge();
			}
		}

		private void SetAlignment(AlignmentEnum alignment)
		{
			switch (alignment)
			{
			case AlignmentEnum.LeftTop:
				this.abtnAlign.mintSelectedItem = 0;
				break;
			case AlignmentEnum.LeftCenter:
				this.abtnAlign.mintSelectedItem = 3;
				break;
			case AlignmentEnum.LeftBottom:
				this.abtnAlign.mintSelectedItem = 6;
				break;
			case AlignmentEnum.CenterTop:
				this.abtnAlign.mintSelectedItem = 1;
				break;
			case AlignmentEnum.CenterCenter:
				this.abtnAlign.mintSelectedItem = 4;
				break;
			case AlignmentEnum.CenterBottom:
				this.abtnAlign.mintSelectedItem = 7;
				break;
			case AlignmentEnum.RightTop:
				this.abtnAlign.mintSelectedItem = 2;
				break;
			case AlignmentEnum.RightCenter:
				this.abtnAlign.mintSelectedItem = 5;
				break;
			case AlignmentEnum.RightBottom:
				this.abtnAlign.mintSelectedItem = 8;
				break;
			}
			this.abtnAlign.Refresh();
		}

		private void RelationRoomName()
		{
			foreach (Element element in this.m_lstElems)
			{
				if (element is Room)
				{
					Room room = element as Room;
					RoomData roomData = this.GetRoomData(room, false);
					this.ReadExtendedData(roomData);
					RoomPracticeForm.m_lstRoomData.Add(roomData);
				}
			}
			foreach (Element element2 in this.m_lstLinkRooms)
			{
				if (element2 is Room)
				{
					Room room2 = element2 as Room;
					RoomData roomData2 = this.GetRoomData(room2, true);
					this.ReadExtendedData(roomData2);
					RoomPracticeForm.m_lstRoomData.Add(roomData2);
				}
			}
			RoomPracticeForm.m_lstRoomData.Sort(new ComparerLevel());
			Dictionary<string, List<RoomData>> dictionary = new Dictionary<string, List<RoomData>>();
			foreach (RoomData roomData3 in RoomPracticeForm.m_lstRoomData)
			{
				if (!dictionary.ContainsKey(roomData3.FloorName))
				{
					dictionary.Add(roomData3.FloorName, new List<RoomData>());
					dictionary[roomData3.FloorName].Add(roomData3);
				}
				else
				{
					dictionary[roomData3.FloorName].Add(roomData3);
				}
			}
			dictionary = this.GetSameFloorRoom(dictionary);
			Dictionary<string, List<RoomData>> dictionary2 = this.Sort(dictionary);
			int num = 0;
			foreach (KeyValuePair<string, List<RoomData>> keyValuePair in dictionary2)
			{
				num += keyValuePair.Value.Count;
			}
			if (num > this.grid.Rows - 3)
			{
				this.grid.Rows = num + 3;
			}
			this.SetDataToFlexCell(dictionary2);
			for (int i = 0; i < this.grid.Rows; i++)
			{
				for (int j = 0; j < this.grid.Cols; j++)
				{
					this.grid.Cell(i, j).Alignment = AlignmentEnum.CenterCenter;
					if (j == 1)
					{
						this.grid.Column(j).AutoFit();
					}
				}
				this.grid.Row(i).AutoFit();
			}
		}

		private Dictionary<string, List<RoomData>> GetSameFloorRoom(Dictionary<string, List<RoomData>> dicRoomData)
		{
			Dictionary<string, List<RoomData>> dictionary = new Dictionary<string, List<RoomData>>();
			foreach (KeyValuePair<string, List<RoomData>> keyValuePair in dicRoomData)
			{
				Dictionary<string, List<RoomData>> dictionary2 = new Dictionary<string, List<RoomData>>();
				foreach (RoomData roomData in keyValuePair.Value)
				{
					if (!dictionary2.ContainsKey(roomData.RoomName))
					{
						dictionary2.Add(roomData.RoomName, new List<RoomData>());
						dictionary2[roomData.RoomName].Add(roomData);
					}
					else
					{
						dictionary2[roomData.RoomName].Add(roomData);
					}
				}
				dictionary.Add(keyValuePair.Key, this.RemoveSameNameRoom(dictionary2));
			}
			return dictionary;
		}

		private List<RoomData> RemoveSameNameRoom(Dictionary<string, List<RoomData>> dicSameNameRoom)
		{
			List<RoomData> list = new List<RoomData>();
			foreach (KeyValuePair<string, List<RoomData>> keyValuePair in dicSameNameRoom)
			{
				bool flag = true;
				foreach (RoomData roomData in keyValuePair.Value)
				{
					if (!roomData.IsLinkRoom && (roomData.FloorNB != "-" || roomData.CeilingNB != "-" || roomData.DatumNB != "-" || roomData.WallSkirtNB != "-" || roomData.SkirtNB != "-" || roomData.InnerWallNB != "-"))
					{
						list.Add(roomData);
						flag = false;
						break;
					}
				}
				if (flag && keyValuePair.Value.Count != 0)
				{
					list.Add(keyValuePair.Value[0]);
				}
			}
			return list;
		}

		private Dictionary<string, List<RoomData>> Sort(Dictionary<string, List<RoomData>> dicRoomData)
		{
			Dictionary<string, List<RoomData>> dictionary = new Dictionary<string, List<RoomData>>();
			foreach (KeyValuePair<string, List<RoomData>> keyValuePair in dicRoomData)
			{
				List<RoomData> value = new List<RoomData>();
				value = (from o in keyValuePair.Value
				orderby o.RoomName
				select o).ToList<RoomData>();
				dictionary.Add(keyValuePair.Key, value);
			}
			return dictionary;
		}

		private RoomData GetRoomData(Room room, bool bIsLink)
		{
			RoomData roomData = new RoomData();
			string empty = string.Empty;
			roomData.RoomElem = room;
			Parameter parameter = room.get_Parameter(BuiltInParameter.ROOM_NAME);
			if (parameter != null)
			{
				roomData.RoomName = parameter.AsString();
			}
			roomData.Level = room.Level.Elevation;
			Parameter parameter2 = room.get_Parameter(BuiltInParameter.LEVEL_NAME);
			if (parameter2 != null)
			{
				roomData.FloorName = parameter2.AsString();
			}
			Parameter parameter3 = room.get_Parameter(BuiltInParameter.ROOM_FINISH_FLOOR);
			if (parameter3 != null)
			{
				if (parameter3.AsString() == null)
				{
					roomData.FloorNB = "-";
				}
				else
				{
					roomData.FloorNB = parameter3.AsString();
				}
			}
			else
			{
				roomData.FloorNB = "-";
			}
			Parameter parameter4 = room.get_Parameter(BuiltInParameter.ROOM_FINISH_BASE);
			if (parameter4 != null)
			{
				if (parameter4.AsString() == null)
				{
					roomData.DatumNB = "-";
				}
				else
				{
					roomData.DatumNB = parameter4.AsString();
				}
			}
			else
			{
				roomData.DatumNB = "-";
			}
			Parameter parameter5 = room.get_Parameter(BuiltInParameter.ROOM_FINISH_CEILING);
			if (parameter5 != null)
			{
				if (parameter5.AsString() == null)
				{
					roomData.CeilingNB = "-";
				}
				else
				{
					roomData.CeilingNB = parameter5.AsString();
				}
			}
			else
			{
				roomData.CeilingNB = "-";
			}
			Parameter parameter6 = room.get_Parameter(BuiltInParameter.ROOM_FINISH_WALL);
			if (parameter6 != null)
			{
				if (parameter6.AsString() == null)
				{
					roomData.InnerWallNB = "-";
				}
				else
				{
					roomData.InnerWallNB = parameter6.AsString();
				}
			}
			else
			{
				roomData.InnerWallNB = "-";
			}
			Parameter parameter7 = room.GetParameter("踢脚面层");
			if (parameter7 != null)
			{
				if (parameter7.AsString() == null)
				{
					roomData.SkirtNB = "-";
				}
				else
				{
					roomData.SkirtNB = parameter7.AsString();
				}
			}
			else
			{
				roomData.SkirtNB = "-";
			}
			Parameter parameter8 = room.GetParameter("墙裙");
			if (parameter8 != null)
			{
				if (parameter8.AsString() == null)
				{
					roomData.WallSkirtNB = "-";
				}
				else
				{
					roomData.WallSkirtNB = parameter8.AsString();
				}
			}
			else
			{
				roomData.WallSkirtNB = "-";
			}
			roomData.IsLinkRoom = bIsLink;
			return roomData;
		}

		private void SetDataToFlexCell(Dictionary<string, List<RoomData>> dicNewRoomData)
		{
			int num = 3;
			int num2 = 1;
			int num3 = 0;
			bool flag = true;
			foreach (KeyValuePair<string, List<RoomData>> keyValuePair in dicNewRoomData)
			{
				this.grid.Cell(num, num2).Text = keyValuePair.Key;
				num3 = num;
				foreach (RoomData roomData in keyValuePair.Value)
				{
					this.grid.Cell(num3, num2 + 1).Text = roomData.RoomName;
					if (flag)
					{
						this.grid.Cell(num3, num2 + 3).Text = roomData.DatumNB;
					}
					else
					{
						this.grid.Cell(num3, num2 + 3).Text = roomData.FloorNB;
					}
					this.grid.Cell(num3, num2 + 2).Text = roomData.Floor;
					this.grid.Cell(num3, num2 + 5).Text = roomData.SkirtNB;
					this.grid.Cell(num3, num2 + 4).Text = roomData.Skirt;
					this.grid.Cell(num3, num2 + 7).Text = roomData.WallSkirtNB;
					this.grid.Cell(num3, num2 + 6).Text = roomData.WallSkirt;
					this.grid.Cell(num3, num2 + 9).Text = roomData.InnerWallNB;
					this.grid.Cell(num3, num2 + 8).Text = roomData.InnerWall;
					this.grid.Cell(num3, num2 + 11).Text = roomData.CeilingNB;
					this.grid.Cell(num3, num2 + 10).Text = roomData.Ceiling;
					this.grid.Cell(num3, num2 + 12).Text = roomData.Remarks;
					if (!RoomPracticeForm.m_dicFloorToRowNumber.ContainsKey(keyValuePair.Key))
					{
						RoomPracticeForm.m_dicFloorToRowNumber.Add(keyValuePair.Key, new List<int>());
						RoomPracticeForm.m_dicFloorToRowNumber[keyValuePair.Key].Add(num3);
					}
					else
					{
						RoomPracticeForm.m_dicFloorToRowNumber[keyValuePair.Key].Add(num3);
					}
					num3++;
				}
				this.grid.Range(num, num2, num3 - 1, num2).Merge();
				num = num3;
				flag = false;
			}
			this.m_nLastRow = num3;
			this.SetColor();
		}

		private void SetColor()
		{
			int num;
			if (this.grid.Rows - 1 > this.m_nLastRow)
			{
				num = this.m_nLastRow;
			}
			else
			{
				num = this.grid.Rows;
			}
			System.Drawing.Color backColor = System.Drawing.Color.FromArgb(160, 232, 232, 232);
			for (int i = 3; i < num; i++)
			{
				if (i % 2 != 0)
				{
					this.grid.Range(i, 2, i, this.grid.Cols - 1).BackColor = backColor;
				}
			}
		}

		private void AddParameter(Element elem)
		{
			if (elem.GetParameter("踢脚面层") == null)
			{
				this.AddSkirting("踢脚面层");
			}
			if (elem.GetParameter("墙裙") == null)
			{
				this.AddSkirting("墙裙");
			}
		}

		private bool AddSkirting(string strParamName)
		{
			string tempFileName = Path.GetTempFileName();
			List<BuiltInCategory> list = new List<BuiltInCategory>();
			list.Add((BuiltInCategory)(-2000160));
			try
			{
                SharedParameterEditor.CreateSharedParams(this.m_Uiapp, tempFileName, strParamName, "标识数据", list, false, (BuiltInParameterGroup)(-5000100));
			}
			catch (Exception)
			{
				return false;
			}
			this.m_Uiapp.ActiveUIDocument.Document.Regenerate();
			return true;
		}

		private void FindPositionInfo()
		{
			int count = RoomPracticeForm.m_lstRoomData.Count;
			int col = 2;
			for (int i = 3; i < this.grid.Rows - 1; i++)
			{
				string value = string.Empty;
				foreach (KeyValuePair<string, List<int>> keyValuePair in RoomPracticeForm.m_dicFloorToRowNumber)
				{
					if (keyValuePair.Value.Contains(i))
					{
						value = keyValuePair.Key;
						break;
					}
				}
				string text = this.grid.Cell(i, col).Text;
				foreach (RoomData roomData in RoomPracticeForm.m_lstRoomData)
				{
					if (roomData.RoomName.Equals(text) && roomData.FloorName.Equals(value))
					{
						string text2 = this.grid.Cell(i, 3).Text;
						if (this.IsFirstFloor(roomData.FloorName))
						{
							roomData.Datum = text2;
						}
						else
						{
							roomData.Floor = text2;
						}
						string text3 = this.grid.Cell(i, 5).Text;
						roomData.Skirt = text3;
						string text4 = this.grid.Cell(i, 7).Text;
						roomData.WallSkirt = text4;
						string text5 = this.grid.Cell(i, 9).Text;
						roomData.InnerWall = text5;
						string text6 = this.grid.Cell(i, 11).Text;
						roomData.Ceiling = text6;
						if (this.IsFirstFloor(roomData.FloorName))
						{
							roomData.DatumNB = this.grid.Cell(i, 4).Text;
						}
						else
						{
							roomData.FloorNB = this.grid.Cell(i, 4).Text;
						}
						roomData.SkirtNB = this.grid.Cell(i, 6).Text;
						roomData.WallSkirtNB = this.grid.Cell(i, 8).Text;
						roomData.InnerWallNB = this.grid.Cell(i, 10).Text;
						roomData.CeilingNB = this.grid.Cell(i, 12).Text;
						roomData.Remarks = this.grid.Cell(i, 13).Text;
					}
				}
			}
		}

		private bool IsFirstFloor(string str)
		{
			string text = this.grid.Cell(3, 1).Text;
			return str == text;
		}

		private void SetParameter(RoomData roomData)
		{
			Room roomElem = roomData.RoomElem;
			Parameter parameter = roomElem.get_Parameter(BuiltInParameter.ROOM_FINISH_FLOOR);
			this.m_bIsFirstFloor = this.IsFirstFloor(roomData.FloorName);
			if (parameter != null && (roomData.FloorNB != "-" || roomData.DatumNB != "-"))
			{
				if (this.m_bIsFirstFloor)
				{
					Parameter parameter2 = roomElem.get_Parameter(BuiltInParameter.ROOM_FINISH_BASE);
					if (parameter2 != null)
					{
						parameter2.Set(roomData.DatumNB);
					}
				}
				else
				{
					parameter.Set(roomData.FloorNB);
				}
			}
			Parameter parameter3 = roomElem.get_Parameter(BuiltInParameter.ROOM_FINISH_WALL);
			if (parameter3 != null && roomData.InnerWallNB != "-")
			{
				parameter3.Set(roomData.InnerWallNB);
			}
			Parameter parameter4 = roomElem.get_Parameter(BuiltInParameter.ROOM_FINISH_CEILING);
			if (parameter4 != null && roomData.CeilingNB != "-")
			{
				parameter4.Set(roomData.CeilingNB);
			}
			Parameter parameter5 = roomElem.GetParameter("墙裙");
			if (parameter5 != null && roomData.WallSkirtNB != "-")
			{
				parameter5.Set(roomData.WallSkirtNB);
			}
			Parameter parameter6 = roomElem.GetParameter("踢脚面层");
			if (parameter6 != null && roomData.SkirtNB != "-")
			{
				parameter6.Set(roomData.SkirtNB);
			}
		}

		private bool ReadExtendedData(RoomData roomData)
		{
			Room roomElem = roomData.RoomElem;
			Schema schema = Schema.Lookup(new Guid(this.m_strGuid));
			if (schema == null)
			{
				return false;
			}
			Entity entity = roomElem.GetEntity(schema);
			if (entity == null)
			{
				return false;
			}
			if (entity.Schema == null)
			{
				return false;
			}
			Field field = schema.GetField("Floor");
			if (field != null)
			{
				roomData.Floor = entity.Get<string>(field);
			}
			Field field2 = schema.GetField("Skirt");
			if (field2 != null)
			{
				roomData.Skirt = entity.Get<string>(field2);
			}
			Field field3 = schema.GetField("WallSkirt");
			if (field3 != null)
			{
				roomData.WallSkirt = entity.Get<string>(field3);
			}
			Field field4 = schema.GetField("InnerWall");
			if (field4 != null)
			{
				roomData.InnerWall = entity.Get<string>(field4);
			}
			Field field5 = schema.GetField("Ceiling");
			if (field5 != null)
			{
				roomData.Ceiling = entity.Get<string>(field5);
			}
			Field field6 = schema.GetField("Remarks");
			if (field6 != null)
			{
				roomData.Remarks = entity.Get<string>(field6);
			}
			return true;
		}

		private void RoomPracticeForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.ReadFormData();
			this.RelationRoomName();
			this.grid.Column(1).Locked = true;
			this.grid.Column(2).Locked = true;
		}

		private void RoomPracticeForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void RoomPracticeForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.WriteFormData();
		}

		private void toolBtnAutomaticTransfer_Click(object sender, EventArgs e)
		{
			this.grid.Selection.WrapText = true;
		}

		private void abtnAlign2_Click(object Sender, AlignButton2.ClickEventArgs e)
		{
			if (this.grid.ActiveCell.ImageKey != "")
			{
				return;
			}
			switch (e.Value)
			{
			case 0:
				this.grid.Selection.Alignment = AlignmentEnum.LeftTop;
				return;
			case 1:
				this.grid.Selection.Alignment = AlignmentEnum.CenterTop;
				return;
			case 2:
				this.grid.Selection.Alignment = AlignmentEnum.RightTop;
				return;
			case 3:
				this.grid.Selection.Alignment = AlignmentEnum.LeftCenter;
				return;
			case 4:
				this.grid.Selection.Alignment = AlignmentEnum.CenterCenter;
				return;
			case 5:
				this.grid.Selection.Alignment = AlignmentEnum.RightCenter;
				return;
			case 6:
				this.grid.Selection.Alignment = AlignmentEnum.LeftBottom;
				return;
			case 7:
				this.grid.Selection.Alignment = AlignmentEnum.CenterBottom;
				return;
			case 8:
				this.grid.Selection.Alignment = AlignmentEnum.RightBottom;
				return;
			default:
				return;
			}
		}

		private void bbtnBorder_Click(object Sender, BorderButton2.ClickEventArgs e)
		{
			switch (e.Value)
			{
			case 0:
				this.grid.Selection.set_Borders((EdgeEnum)255, LineStyleEnum.None);
				return;
			case 1:
				this.grid.Selection.set_Borders(EdgeEnum.Top, LineStyleEnum.Thin);
				return;
			case 2:
				this.grid.Selection.set_Borders(EdgeEnum.Bottom, LineStyleEnum.Thin);
				return;
			case 3:
				this.grid.Selection.set_Borders(EdgeEnum.Left, LineStyleEnum.Thin);
				return;
			case 4:
				this.grid.Selection.set_Borders(EdgeEnum.Right, LineStyleEnum.Thin);
				return;
			case 5:
				this.grid.Selection.set_Borders(EdgeEnum.DiagonalUp, LineStyleEnum.Thin);
				return;
			case 6:
				this.grid.Selection.set_Borders(EdgeEnum.DiagonalDown, LineStyleEnum.Thin);
				return;
			case 7:
				this.grid.Selection.set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
				return;
			case 8:
				this.grid.Selection.set_Borders(EdgeEnum.Outside, LineStyleEnum.Thin);
				return;
			case 9:
				this.grid.Selection.set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
				return;
			case 10:
				this.grid.Selection.set_Borders((EdgeEnum)207, LineStyleEnum.Thin);
				return;
			case 11:
				this.grid.AutoRedraw = false;
				this.grid.Selection.set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
				this.grid.Selection.set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
				this.grid.AutoRedraw = true;
				this.grid.Refresh();
				return;
			default:
				return;
			}
		}

        private void grid_RowColChange(object Sender, FlexCell.Grid.RowColChangeEventArgs e)
		{
			this.toolCmbFont.Text = this.grid.Cell(e.Row, e.Col).Font.Name;
			this.toolCmbFontSize.Text = this.grid.Cell(e.Row, e.Col).Font.Size.ToString();
			this.SetAlignment(this.grid.Cell(e.Row, e.Col).Alignment);
		}

		private void toolCmbFont_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				this.grid.Selection.FontName = this.toolCmbFont.Text;
				this.grid.Refresh();
			}
			catch
			{
			}
		}

		private void toolCmbFontSize_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.grid.Selection.FontSize = (float)int.Parse(this.toolCmbFontSize.Text);
			this.grid.Refresh();
		}

		private void toolBtnThickening_Click(object sender, EventArgs e)
		{
			this.grid.Selection.FontBold = !this.grid.ActiveCell.Font.Bold;
		}

		private void toolBtnItalic_Click(object sender, EventArgs e)
		{
			this.grid.Selection.FontItalic = !this.grid.ActiveCell.Font.Italic;
		}

		private void toolBtnUnderline_Click(object sender, EventArgs e)
		{
			this.grid.Selection.FontUnderline = !this.grid.ActiveCell.Font.Underline;
		}

		private void toolBtnDeleteText_Click(object sender, EventArgs e)
		{
			this.grid.Selection.FontStrikeout = !this.grid.ActiveCell.Font.Strikeout;
		}

		private void cbtnBackColor_Click(object Sender, EventArgs e)
		{
			this.grid.Selection.BackColor = ColorTranslator.FromWin32(this.cbtnBackColor.Value);
		}

		private void cbtnFontColor_Click(object Sender, EventArgs e)
		{
			this.grid.Selection.ForeColor = ColorTranslator.FromWin32(this.cbtnFontColor.Value);
		}

		private void btnRoomPracticeAssignment_Click(object sender, EventArgs e)
		{
			Transaction transaction = new Transaction(this.m_doc, "SetParameter");
			try
			{
				if (this.DataIsNull())
				{
					YJKMessageBox.Show("表格中无做法信息");
				}
				else
				{
					this.FindPositionInfo();
					transaction.Start();
					foreach (RoomData roomData in RoomPracticeForm.m_lstRoomData)
					{
						if (!roomData.IsLinkRoom)
						{
							this.AddParameter(roomData.RoomElem);
							this.SetParameter(roomData);
							this.WriteExtendedData(roomData);
						}
					}
					transaction.Commit();
					YJKMessageBox.Show("当前文档房间赋值成功");
				}
			}
			catch (Exception)
			{
				if (transaction.IsValidObject && transaction.HasStarted())
				{
					transaction.RollBack();
				}
			}
		}

		private bool DataIsNull()
		{
			for (int i = 3; i < this.grid.Rows; i++)
			{
				for (int j = 3; j < this.grid.Cols; j++)
				{
					Cell cell = this.grid.Cell(i, j);
					if (cell.Text != "-" && cell.Text != "")
					{
						return false;
					}
				}
			}
			return true;
		}

		private void WriteExtendedData(RoomData roomData)
		{
			Guid guid = new Guid(this.m_strGuid);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				this.ProvideSparyschemaData(ref schema, guid);
			}
			if (schema == null)
			{
				return;
			}
			Entity entity = new Entity(schema);
			this.ProvideEntityData(ref entity, schema, roomData);
			roomData.RoomElem.SetEntity(entity);
		}

		private void ProvideSparyschemaData(ref Schema Sparyschema, Guid Sparyguid)
		{
			SchemaBuilder schemaBuilder = new SchemaBuilder(Sparyguid);
			schemaBuilder.SetReadAccessLevel((AccessLevel)1);
			schemaBuilder.SetWriteAccessLevel((AccessLevel)1);
			schemaBuilder.SetSchemaName("HY_RoomPractice_Data");
			schemaBuilder.AddSimpleField("Floor", typeof(string));
			schemaBuilder.AddSimpleField("Skirt", typeof(string));
			schemaBuilder.AddSimpleField("WallSkirt", typeof(string));
			schemaBuilder.AddSimpleField("InnerWall", typeof(string));
			schemaBuilder.AddSimpleField("Ceiling", typeof(string));
			schemaBuilder.AddSimpleField("Remarks", typeof(string));
			Sparyschema = schemaBuilder.Finish();
		}

		private void ProvideEntityData(ref Entity entity, Schema Sparyschema, RoomData roomData)
		{
			Field field = Sparyschema.GetField("Floor");
			if (this.m_bIsFirstFloor)
			{
				entity.Set<string>(field, roomData.Datum);
			}
			else
			{
				entity.Set<string>(field, roomData.Floor);
			}
			Field field2 = Sparyschema.GetField("Skirt");
			entity.Set<string>(field2, roomData.Skirt);
			Field field3 = Sparyschema.GetField("WallSkirt");
			entity.Set<string>(field3, roomData.WallSkirt);
			Field field4 = Sparyschema.GetField("InnerWall");
			entity.Set<string>(field4, roomData.InnerWall);
			Field field5 = Sparyschema.GetField("Ceiling");
			entity.Set<string>(field5, roomData.Ceiling);
			Field field6 = Sparyschema.GetField("Remarks");
			entity.Set<string>(field6, roomData.Remarks);
		}

        private void grid_LeaveCell(object Sender, FlexCell.Grid.LeaveCellEventArgs e)
		{
			for (int i = 1; i < this.grid.Rows; i++)
			{
				this.grid.Row(i).AutoFit();
			}
		}

		private void btnExportExcell_Click(object sender, EventArgs e)
		{
			string fileName = string.Empty;
			string text = this.m_doc.Title;
			int num = text.IndexOf('.');
			if (num != -1)
			{
				text = text.Substring(0, num);
			}
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Filter = "Excel文件|*.xls|盈建科表格文件|*.hybg";
			saveFileDialog.FileName = text + "-材料做法表";
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				try
				{
					File.OpenWrite(saveFileDialog.FileName).Close();
				}
				catch (Exception)
				{
					YJKMessageBox.Show("请先关闭当前导入文档!");
					base.DialogResult = DialogResult.Cancel;
					return;
				}
				fileName = saveFileDialog.FileName;
				this.grid.ExportToExcel(fileName, false, false);
				YJKMessageBox.Show("数据已导出!");
				base.Close();
			}
		}

		private void btnGenerateTable_Click(object sender, EventArgs e)
		{
			this.FindPositionInfo();
			base.DialogResult = DialogResult.OK;
		}

		private ExternalCommandData m_cmdData;

		private Document m_doc;

		private UIApplication m_Uiapp;

		private List<Element> m_lstElems = new List<Element>();

		public static List<RoomData> m_lstRoomData;

		public static Dictionary<string, List<int>> m_dicFloorToRowNumber;

		private List<Element> m_lstLinkRooms = new List<Element>();

		public bool m_bIsExit = true;

		private int m_nLastRow;

		private int m_nVersionNumber;

		private bool m_bIsFirstFloor;

		public string m_strGuid = "C38E6AD5-84EE-4150-86AD-EED640E4C734";
	}
}
